//
//  FHScrollTableViewHeaderFooterView.m
//  ZhiXinFM
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 YITeam. All rights reserved.
//

#import "FHScrollTableViewHeaderFooterView.h"
#import "FHScrollViewModels.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "FHScorllViewUrl.h"

#define ImageCount  3

@interface FHScrollTableViewHeaderFooterView()<UIScrollViewAccessibilityDelegate>
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,weak)UIButton *ButtonNewFm;
@property(nonatomic,weak)NSMutableArray *imageArray;
@property(nonatomic,strong)NSMutableArray *scrollArray;
@end

@implementation FHScrollTableViewHeaderFooterView

- (void)setCentertmodels:(FHScrollViewModels *)centertmodels{
    _centertmodels=centertmodels;
        [self.centerIamge sd_setImageWithURL:[NSURL URLWithString:self.centertmodels.cover] placeholderImage:[UIImage imageNamed:@"wfh123"]];
}
- (void)setLeftmodels:(FHScrollViewModels *)leftmodels{
    _leftmodels=leftmodels;
    [self.leftIamge sd_setImageWithURL:[NSURL URLWithString:self.leftmodels.cover] placeholderImage:[UIImage imageNamed:@"wfh123"]];
}
- (void)setRightmodels:(FHScrollViewModels *)rightmodels{
    _rightmodels=rightmodels;
    [self.rightIamge sd_setImageWithURL:[NSURL URLWithString:self.rightmodels.cover] placeholderImage:[UIImage imageNamed:@"wfh123"]];
    
}

//便利构造方法
+ (instancetype)headerViewWithTableView:(UITableView *)tableview{
    static NSString *headImage=@"header";
    FHScrollTableViewHeaderFooterView * head=[tableview dequeueReusableCellWithIdentifier:headImage];
    if (head==nil) {
        head =[[FHScrollTableViewHeaderFooterView alloc]initWithReuseIdentifier:headImage];
    }
    
    return head;
}

//重写初始化方法，向标题区域添加内部的子控件
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self==[super initWithReuseIdentifier:reuseIdentifier]) {
        UIScrollView *scrollView=[[UIScrollView alloc]init];
        UIPageControl *pageControl=[[UIPageControl alloc]init];
        UIButton *btn=[[UIButton alloc]init];
        pageControl.numberOfPages=ImageCount;
        pageControl.currentPage=0;
        [self.contentView addSubview:btn];
        [self.contentView addSubview:scrollView];
        [self.contentView addSubview:pageControl];
        self.scrollView=scrollView;
        self.pageControl=pageControl;
        self.ButtonNewFm=btn;
        self.scrollView.delegate=self;
        [self addTimer];
    }
    self.imageArray=[NSMutableArray array];
    [self requestScrollInfo];
    return self;
}
#pragma mark -获取轮播器的数据
- (void)requestScrollInfo{
    //self.view.backgroundColor=[UIColor purpleColor];
    AFHTTPRequestOperationManager *mgr=[[AFHTTPRequestOperationManager alloc]init];
    [mgr GET:SCROLLVIEWIMAGE parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // NSLog(@"%@",responseObject);
        self.scrollArray=[NSMutableArray array];
        for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
            FHScrollViewModels *models=[FHScrollViewModels scrollviewWithDic:dic];
            [self.scrollArray addObject:models];
            //NSLog(@"----------%@",models.cover);
        }
        //NSLog(@"%@",self.scrollArray);
        self.leftmodels=self.scrollArray[0];
        self.centertmodels=self.scrollArray[1];
        self.rightmodels=self.scrollArray[2];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -布局内部的子控件
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat scrollX=0;
    CGFloat scrollY=0;
    CGFloat scrollW=self.frame.size.width;
    CGFloat scrollH=180;
    CGFloat pageX=160;
    CGFloat pageY=150;
    CGFloat pageW=50;
    CGFloat pageH=50;
    self.scrollView.frame=CGRectMake(scrollX, scrollY, scrollW, scrollH);
    self.pageControl.frame=CGRectMake(pageX, pageY, pageW, pageH);
    CGFloat imageW=self.scrollView.frame.size.width;
    CGFloat imageH=self.scrollView.frame.size.height;
    CGFloat imageY=0;

    UIImageView *imageView=[[UIImageView alloc]init];
    CGFloat imageX=0;
    imageView.frame=CGRectMake(imageX, imageY, imageW, imageH);
    [self.scrollView addSubview:imageView];
    self.leftIamge=imageView;

    UIImageView *imageView2=[[UIImageView alloc]init];
    CGFloat imageX2=imageW;
    imageView2.frame=CGRectMake(imageX2, imageY, imageW, imageH);
    [self.scrollView addSubview:imageView2];
    self.centerIamge=imageView2;
    
    UIImageView *imageView3=[[UIImageView alloc]init];
    CGFloat imageX3=2*imageW;
    imageView3.frame=CGRectMake(imageX3, imageY, imageW, imageH);
    [self.scrollView addSubview:imageView3];


    CGFloat contentW=ImageCount*self.scrollView.frame.size.width;
    self.scrollView.contentSize=CGSizeMake(contentW, 0);
    self.scrollView.showsHorizontalScrollIndicator=NO;
    self.scrollView.pagingEnabled=YES;
    
    self.ButtonNewFm.frame=CGRectMake(0, CGRectGetMaxY(self.scrollView.frame)+20,scrollW ,44 );
    self.ButtonNewFm.backgroundColor=[UIColor orangeColor];
    UIImageView *imageicon=[[UIImageView alloc]init];
    [self.ButtonNewFm addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [self.ButtonNewFm setTitle:@"最新FM" forState:UIControlStateNormal];
    [self.ButtonNewFm setTintColor:[UIColor blackColor]];
    self.ButtonNewFm.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.ButtonNewFm.contentEdgeInsets=UIEdgeInsetsMake(0, 20, 0, 0);
    imageicon.frame=CGRectMake(340, 10, 15, 15);
    imageicon.image=[UIImage imageNamed:@"broad"];
    [self.ButtonNewFm addSubview:imageicon];
    
}
- (void)jump{
    NSLog(@"sssddddds");
}
//添加定时器
- (void)addTimer{
    //添加定时器(每隔2秒调用一次)
    self.timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

//移除定时器
- (void)removeTimer{
    [self.timer invalidate];
    self.timer=nil;
    
}

//设置图片的轮播效果
- (void)nextImage{
    //1.增加pageControl的页面
    int page=0;
    if (self.pageControl.currentPage==ImageCount-1) {
        page=0;
    }else{
        page=(int)self.pageControl.currentPage+1;
    }
    //计算scrollview滚动的位置
    CGFloat offsetX=page*self.scrollView.frame.size.width;
    CGPoint offset=CGPointMake(offsetX, 0);
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark -scrollview的代理方法
//scrollView拖动的时候会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat scrollW=self.scrollView.frame.size.width;
    int page=(self.scrollView.contentOffset.x+scrollW*0.5)/scrollW;
    self.pageControl.currentPage=page;
}

//开始拖拽的时候移除定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}

//停止拖拽的时候添加定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    if (scrollView == self.tableview)
//    {
//        CGFloat sectionHeaderHeight = 50;
//        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
//        {
//            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y,0, 0, 0);
//        } else if (scrollView.contentOffset.y>=sectionHeaderHeight)
//        {
//            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//        }
//    }
//}
@end
